﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace GraffitiExtras.Plugins.Waegis.WaegisClient.Components
{
    /// <summary>
    /// Deserilizes the response XML of Waegis API
    /// </summary>
    internal static class WaegisDeserializer
    {
        #region Public Methods

        /// <summary>
        /// Deserializes the response to a ServiceResult object
        /// </summary>
        /// <param name="serviceResult">The string value returned from Waegis API</param>
        /// <returns>An instance of ServiceResult</returns>
        public static ServiceResult GetResultFromData(string serviceResult)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(serviceResult);

            XmlNode status = xml.SelectSingleNode("result/status");
            XmlNode message = xml.SelectSingleNode("result/message");

            ServiceResult result = new ServiceResult();

            if (status.InnerText == "success")
                result.Status = ResultStatus.Success;
            else
                result.Status = ResultStatus.Failure;

            result.Message = message.InnerText;

            return result;
        }

        #endregion
    }
}
